package Commun;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;

public class Connexion {
    private ArrayList<String> prop;
    private BufferedReader lec;
    private Connection conn;
    private static Connexion instance = new Connexion("./Properties.txt");

    private Connexion(String fic){
        try{
            lec = new BufferedReader(new InputStreamReader(new FileInputStream(fic)));
            prop = new ArrayList<String>();
            String line;
            while((line=lec.readLine())!=null){
                prop.add(line);
                System.out.println(line);
            }
            Class.forName(prop.get(0));
            conn = DriverManager.getConnection(prop.get(1)+"/"+prop.get(2)+"?user=" +prop.get(3) +"&password=" +prop.get(4));
            lec.close();
        }catch(IOException e){
            System.err.println("Probleme de chargement du fichier: "+e);
        }catch(ClassNotFoundException f){
            System.err.println("Erreur lors du chargement du pilote : "+f);
        }catch(SQLException g){
            System.out.println("SQLException: " + g.getMessage());
            System.out.println("SQLState: " + g.getSQLState());
            System.out.println("VendorError: " + g.getErrorCode());
        }
    }

    public static Connection getConnexion(){
        return instance.conn;
    }
}
